import TableLyout from '@/components/commonPublic/tableLyout.vue'
import TableData from "@/components/public/el-table-pagination"; //主页面列表
import CustomDialog from "@/components/PhysicalQualityPage/reboundStrengthManage/detectionCriteria/index.vue";
import publicForm from "@/components/materialDataManage/fromCom.vue"; //弹框form
import elBreadcrumb from "@/components/public/el-breadcrumb.vue";
import formJs from "./regexDetail.js"; //新增弹框配置
import tree from "@/components/PhysicalQualityPage/rebarThicknessManage/el-tree"; //左侧机构树
import {
    getChecknodeList, //树状图接口
    getCheckitemsPageList, //列表
} from "@/api/request.js";

export default {
    name: "rebarDetectionCriteria",
    data() {
        return {
            name: "rebarDetectionCriteria",
            treeText: {
                //树状图参数
                companyId: null,
                userId: null,
            },
            // 左侧机构树
            defaultProps: {
                children: "children",
                label: "fName",
            },
            checkData: [],
            treeData: [], //左侧树状图数据
            showName: "未选择公司",
            treelength: false,
            // 列表
            getList: {
                modelType: "",
            },
            expandRows: [], // 展开数组

            isLock: false,
            projectId: null,
            projectName: null,
            loading: false,
            btnObj: [], //按钮
            queryCriteriaData: {}, //分页
            navigatepageNums: null,
            searchObj: formJs.searchObj, //搜索条件
            table_config: formJs.table_config, //列表


            isShow: false, //是否显示弹框

            statusNum: null, //点击那个按钮
            dataFid: null, //列表详情id
            currentRow: null, //列表详情
            activeIndex: "1",
            fId: null, //当前数据fId
            tableData: [],
            companyId: null,
            currentRowId: "",
            dialogTitle: null,
        };
    },
    computed: {
        hIndex() {
            // 当前页数 - 1 * 每页数据条数 + 1
            return (this.getList.page - 1) * this.getList.pageSize + 1
        }
    },
    components: {
        TableLyout,
        publicForm,
        TableData,
        elBreadcrumb,
        CustomDialog,
        tree
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        // this.yj_searchForm(this.getList, 1);
        this.btnRole(this.name)
        this.getTree()
    },
    methods: {
        // 页面切换
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.getList.page = val;
            this.yj_searchForm(this.getList);
        },
        // 左侧树结构
        getTree() {
            this.treeText.companyId = this.userInfo.companyId;
            this.treeText.userId = this.userInfo.userId;

            getChecknodeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.checknodeList;
                        this.treeData = this.tranListToTreeData(companyTree)[1].children; //将接口返回数据二次处理，形成树状图结构
                        this.showName = this.treeData[0].fName; //当前点击组织机构名称

                        this.getList.modelType = this.treeData[0].fId; //当前点击组织机构ID
                        this.checkData[0] = this.treeData[0].fId;


                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // modelType
                    } else {
                        this.$message.error("机构树", res.msg);
                    }
                })
                .catch(() => {
                    console.log("机构树");
                });
        },
        tranListToTreeData(data) {
            let result = [];
            if (!Array.isArray(data)) {
                return result;
            }
            // 清除已存在的children 重新赋值
            data.forEach((item) => {
                delete item.children;
            });

            let map = {};
            data.forEach((item) => {
                map[item.fId] = item;
            });

            data.forEach((item) => {
                let parent = map[item.fParentid];
                if (parent) {
                    (parent.children || (parent.children = [])).push(item);
                } else {
                    result.push(item);
                }
            });
            return result;
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fName; //顶部显示机构名
            this.getList.modelType = data.fId;
            this.yj_searchForm(this.getList);
        },
        refreshList(status) { //刷新列表
            if (status == 0) {
                this.isShow = true;
            }
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            })
        },
        closeDialog() {
            this.isShow = false;
        },
        handleRow() {},
        btnRole(name) {
            if (this.userInfo.userId != 'System') {
                var btnList = [];
                var ModuleList = JSON.parse(sessionStorage.getItem("yjSmart-ModuleList"));
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map(item => {
                        item['disabled'] = false
                    })
                    this.btnObj = btnList
                }
            } else {
                this.btnObj = formJs.btnObj
            }
        },
        getBtnName(name, status) {
            this.statusNum = status
            this.yj_edit(status)
        },

        // 点击查看
        yj_viewForm(row, label) {
            if (label == "标准代号") {
                this.statusNum = 7
                this.isShow = true;
                this.currentRow = row;
                this.dialogTitle = '查看'


            } 
        },
        // 新增/编辑
        yj_edit(num) {
            // if (this.userInfo.userId == 'System') {
                if (num == 7) { //查看
                    if (this.dataFid != null) {
                        if (this.currentRow.materialList) {
                            this.dialogTitle = '材料种类查看'
                        } else {
                            this.dialogTitle = '材料名称查看'
                        }
                        this.statusNum = 7
                        this.isShow = true;

                    } else {
                        this.$message.warning("请先选择需要查看的数据");
                    }
                }
            // } else {
            //     this.$message.warning("该用户无权限");
            // }
        },
        editText() {
            this.isShow = true;
        },


        // 查看

        // 点击当前行变色
        rowChange(val, expandedRows) {
            this.currentRow = val
            this.dataFid = val.fId;
            if (expandedRows.length > 1) {
                this.$refs.chartTable.toggleRowExpansion(expandedRows[0])
            }
        },
        // 获取列表数据
        yj_searchForm(data) {
            console.log('列表', data);
            // var obj = {
            //     modelType: data.modelType,

            // }
            this.loading = true;
            var that = this;
            getCheckitemsPageList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.checkitemsList.list;
                        that.queryCriteriaData = res.data.checkitemsList; //分页信息
                        that.navigatepageNums =
                            res.data.checkitemsList.navigatepageNums.length;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
    },
};